﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.DAL;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.DAL.Model.Sys;

namespace XQ.WeChatOfficialAccount.BLL.Sys
{
    public class UserBLL
    {
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public List<UserExModel> GetUserList(int pageSize, int index, string userName, string trueName, Pagination page, int deptID=1)
        {
            UserDal dal = new UserDal();
            return dal.GetUserList(pageSize, index, userName, trueName, deptID, page);
        }

        /// <summary>
        /// 根据ID数据获取用户列表
        /// </summary>
        /// <returns></returns>
        public List<Sys_Users> GetUsersByIds(List<int> userIds)
        {
            UserDal dal = new UserDal();
            return dal.GetUsersByIds(userIds);
        }

        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <returns></returns>
        public string GetUsersNum(string userName, string trueName)
        {
            UserDal dal = new UserDal();
            return dal.GetUsersNum(userName, trueName);
        }

        /// <summary>
        /// 新增或编辑用户
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        public int SaveUser(Sys_Users user)
        {            
            //保存用户
            int userid = 0;
            UserDal dal = new UserDal();
            if (user.id == 0)
            {
                userid = dal.SaveUser(user);
            }
            else
            {
                userid = dal.UpdateUser(user);
            }
            return userid;
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        public int UpdateUser(Sys_Users userModel)
        {
            UserDal dal = new UserDal();
            return dal.SaveUser(userModel);
        }

        public Sys_Users GetUserID(int userid)
        {
            UserDal dal = new UserDal();
            return dal.GetUserByID(userid);
        }

        public UserExModel GetUserByOpenid(string openid)
        {
            UserDal dal = new UserDal();
            return dal.GetUserByOpenid(openid);
        }

        /// <summary>
        /// 删除用户（标识删除）
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public bool DeleteUser(int userid)
        {
            UserDal dal = new UserDal();
            return dal.DeleteUser(userid);
        }

        /// <summary>
        /// 删除用户(多删除)
        /// </summary>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public string DeleteUsers(List<int> userIds)
        {
            UserDal dal = new UserDal();
            if (dal.DeleteUsers(userIds))
            {
                return "success";
            }
            return "err";
        }

        /// <summary>
        /// 根据用户ID获取对应权限ID
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public int GetRoleByUserID(int userid)
        {
            UserDal dal = new UserDal();
            return dal.GetRoleByUserID(userid);
        }

        /// <summary>
        /// 保存用户对应的权限
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public bool SaveUserRole(int userid, int roleid)
        {
            UserDal dal = new UserDal();
            return dal.SaveUserRole(userid, roleid);
        }

        /// <summary>
        /// 根据部门和角色获取用户列表
        /// </summary>
        /// <param name="departmentId"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<UserExModel> GetUserList(int departmentId, int roleId)
        {
            return new UserDal().GetUserList(departmentId, roleId);
        }

        /// <summary>
        /// 密码更改
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="oldPwd">旧密码</param>
        /// <param name="newPwd">新密码</param>
        /// <returns></returns>
        public string ChangePwd(int userId, string oldPwd, string newPwd)
        {
            UserDal userDal = new UserDal();
            if (!userDal.CheckPwdIsRight(userId, oldPwd))
            {
                return "原密码错误！";
            }
            else
                if (userDal.ChangePwd(userId, newPwd))
                {
                    return "密码更改成功！";
                }
                else
                {
                    return "密码更新失败！";
                }
        }


        //获取指定部门的人员
        public List<Sys_Users> GetUsersByDeptID(int deptId)
        {
            List<Sys_Users> list = new UserDal().GetUsersByDeptID(deptId);

            return list;
        }

        public List<int> GetMenusByUser(int userid)
        {
            RoleMenusDal dal = new RoleMenusDal();
            return dal.GetMenusByUser(userid);
        }
    }

}
